package com.qingcheng.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qingcheng.entity.Result;
import com.qingcheng.service.order.CartService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author Liu Chengkun
 * @description
 * @date 2019/9/16 0016
 */
@RestController
@RequestMapping("/cart")
public class CartController {

    @Reference
    private CartService cartService;

//    String username = "menjin";

    /**
     * 查询购物车
     * @return
     */
    @GetMapping("/findCartList")
    public List<Map<String,Object>> findCartList(){

      String username = SecurityContextHolder.getContext().getAuthentication().getName();




        return cartService.findCartList(username);
    }

    /**
     * 保存
     * @param id
     * @param num
     * @return
     */
    @GetMapping("/save")
    public Result saveGoodToCart(String id, Integer num){
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        cartService.saveGoodsToCart(username,id,num);
        return new Result();
    }

    /**功能描述
     * 商品详情与购物车的结合
     * 添加商品到购物车
     * @date 2019/9/17 0017
     * @param
     * @return
     */
    @GetMapping("/buy")
    public void toCartList(HttpServletResponse response, String skuId, Integer num) throws IOException {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        cartService.saveGoodsToCart(username,skuId,num);
        response.sendRedirect("/cart.html");
    }


    /**
     * 修改商品选中状态
     * @param skuId
     * @param checked
     * @return
     */
    @GetMapping("/updateChecked")
    public Result updateChecked(String skuId, boolean checked){
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        cartService.updateChecked(username,skuId,checked);
        return new Result();
    }


}
